package com.andaily.domain.dto;

import com.andaily.infrastructure.mybatis.data.AccountStatisticsData;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Date: 13-6-27 上午12:07
 *
 * @author Shengzhao Li
 */
public abstract class AbstractChartDto<T> extends AbstractDTO {

    //default chart date size 30
    public static final int DEFAULT_CHART_DATA_SIZE = 30;

    protected List<T> list = new ArrayList<T>();

    //default 25
    protected int scaleStepWidth = 25;
    //fixed value
    protected final int scaleSteps = 30;

    protected AbstractChartDto() {
    }


    protected void initialStepWidth(List<AccountStatisticsData> dataList) {
        final int size = dataList.size();
        if (size > 1) {
            BigDecimal maxAmount = dataList.get(0).getAmount();
            BigDecimal minAmount = maxAmount;
            for (AccountStatisticsData statisticsData : dataList) {
                final BigDecimal amount = statisticsData.getAmount();
                if (amount.compareTo(maxAmount) > 0) {
                    maxAmount = amount;
                }
                if (amount.compareTo(minAmount) < 0) {
                    minAmount = amount;
                }
            }

            final BigDecimal tempAmount = maxAmount.subtract(minAmount)
                    .divide(new BigDecimal(String.valueOf(this.scaleSteps)), 2, BigDecimal.ROUND_HALF_EVEN);
            final int tempStepWidth = tempAmount.intValue();
            if (tempStepWidth > this.scaleStepWidth) {
                this.scaleStepWidth = tempStepWidth + 15;
            }
        }
    }

    public int getChartDataSize() {
        return DEFAULT_CHART_DATA_SIZE;
    }

    public final int getScaleSteps() {
        return scaleSteps;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }


    public int getScaleStepWidth() {
        return scaleStepWidth;
    }

    public void setScaleStepWidth(int scaleStepWidth) {
        this.scaleStepWidth = scaleStepWidth;
    }
}
